package org.bonorsoft.modules.education.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.system.api.ISysBaseAPI;
import org.bonorsoft.common.util.WilliamUtil;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.education.entity.EduInfor;
import org.bonorsoft.modules.education.mapper.EduInforMapper;
import org.bonorsoft.modules.education.service.IOutStaticService;
import org.bonorsoft.modules.statistical.entity.StatisticalBasicData;
import org.bonorsoft.modules.statistical.out.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class OutStaticServiceImpl extends ServiceImpl<EduInforMapper, EduInfor> implements IOutStaticService {
    @Resource
    private EduInforMapper eduInforMapper;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    /**
     * 获取全部统计数据
     * @param year
     * @param type
     * @return
     */
//    @Cacheable(value = CacheConstant.SYS_DICT_OUT_STATIC,key = "#year+'-'+#type")
    @Override
    public List<StatisticalBasicData> getAllStructure(Integer year, String type) {
        log.info("无缓存调用！");
        List<StatisticalBasicData> list;
        if(oConvertUtils.isEmpty(type)){
            //查询统计全部学段（默认2019，全部学段）
            list = eduInforMapper.getAllStructure(year);
        }else{
            //根据学段类型查询统计（默认2019）
            list = eduInforMapper.getAllStructureByType(year, type);
        }
        return list;
    }

    @Override
    public List<SchoolsNumber> getSchoolsNumber() {
        List<SchoolsNumber> list = eduInforMapper.getSchoolsNumber();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<Qualifications> getQualifications() {
        List<Qualifications> list = eduInforMapper.getQualifications();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<AgeStructure> getAgeStructure() {
        List<AgeStructure> list = eduInforMapper.getAgeStructure();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<TitleStructure> getTitleStructure() {
        List<TitleStructure> list = eduInforMapper.getTitleStructure();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<TeacherNumber> getTeacherNumber() {
        List<TeacherNumber> list = eduInforMapper.getTeacherNumber();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<InSchoolNumber> getInSchoolNumber() {
        List<InSchoolNumber> list = eduInforMapper.getInSchoolNumber();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<StaffStudentRatio> getStaffStudentRatio() {
        List<StaffStudentRatio> list = eduInforMapper.getStaffStudentRatio();
        list.forEach(item -> {
            item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13"));
            String percentage = WilliamUtil.getSchoolPercentage(item.getStaffNumber(), item.getStudentNumber());
            if (!percentage.contains(".")){
                percentage = "0";
            }
            item.setRatio(percentage);
        });
        return list;
    }

    @Override
    public List<BoyGirlRatio> getBoyGirlRatio() {
        List<BoyGirlRatio> list = eduInforMapper.getBoyGirlRatio();
        list.forEach(item -> {
            item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13"));
            String percentage = WilliamUtil.getSchoolPercentage(item.getBoyNumber(), item.getGirlNumber());
            if (!percentage.contains(".")){
                percentage = "0";
            }
            item.setRatio(percentage);
        });
        return list;
    }

    @Override
    public List<SubjectTeacher> getSubjectTeacher() {
        List<SubjectTeacher> list = eduInforMapper.getSubjectTeacher();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<ExamStudent> getExamStudent(Integer year) {
        List<ExamStudent> list = eduInforMapper.getExamStudent(year);
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<InSchoolStudent> getInSchoolStudent() {
        return eduInforMapper.getInSchoolStudent();
    }

    @Override
    public List<GraduationStudent> getGraduationStudent() {
        List<GraduationStudent> list = eduInforMapper.getGraduationStudent();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<AgeStudent> getAgeStudent() {
        return eduInforMapper.getAgeStudent();
    }

    @Override
    public List<PrivateSchool> getPrivateSchool() {
        List<PrivateSchool> list = eduInforMapper.getPrivateSchool();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<FullTimeFaculty> getFullTimeFaculty() {
        List<FullTimeFaculty> list = eduInforMapper.getFullTimeFaculty();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<ForeignStudent> getForeignStudent() {
        List<ForeignStudent> list = eduInforMapper.getForeignStudent();
        list.forEach(item -> item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13")));
        return list;
    }

    @Override
    public List<AverageClass> getAverageClass() {
        List<AverageClass> list = eduInforMapper.getAverageClass();
        list.forEach(item -> {
            item.setLearn(sysBaseAPI.getLearnType(item.getLearn(), "13"));
            String percentage = WilliamUtil.getSchoolPercentage(item.getTotalStudent(), item.getClassNumber());
            if (!percentage.contains(".")){
                percentage = "0";
            }
            item.setAverage(percentage);
        });
        return list;
    }

    @Override
    public List<InstitutionNumbers> getInstitutionNumbers() {
        return eduInforMapper.getInstitutionNumbers();
    }

}
